<?php
declare (strict_types=1);

namespace app\sysuser\controller\say;

use app\common\controller\SysBackend;
use app\common\enums\say\SayCategoryStatusEnum;
use app\common\model\banxi\say\SayCategory as SayCategoryModel;
use think\db\exception\DbException;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

class Category extends SysBackend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new SayCategoryModel();
    }


    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 分页Select查询
     * @return void
     * @throws DbException
     */
    public function listSelectPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query", 'searchText']);
        if (!empty($param['searchText'])) {
            $param['query']['LIKE_name'] = $param['searchText'];
        }
        $param['query']['EQ_status'] = SayCategoryStatusEnum::E1;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListSelectPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }

    /**
     * 新增或修改
     * @return void
     */
    public function saveOrUpdate()
    {
        $param = $this->request->param(['id' => null, 'name' => '', 'type' => 1, 'status' => 1, 'sortNum' => 0]);
        try {
            $validate = Validate::rule([
                'name' => ValidateRule::isRequire(null, '请输入年级阶段名称'),
                'type' => ValidateRule::IN([1, 2], '请设置正确的类型'),
                'sortNum' => ValidateRule::isNumber(null, '请输入正确的排序数字'),
                'status' => ValidateRule::IN([1, 2], '请设置正确的状态')
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->saveOrUpdate($param);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }


    public function del()
    {
        $param = $this->request->param(['ids' => []]);
        try {
            $validate = Validate::rule([
                'ids' => ValidateRule::isRequire(null, '请选择你要删除的记录'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->del($param['ids']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }
}
